package edu.neu.swc.commerce.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import edu.neu.swc.commerce.blockHandler.QinyiBlockHandler;
import edu.neu.swc.commerce.vo.CommonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <h1>基于Sentinel控制台配置流控规则</h1>
 * Sentinel 是懒加载，先去访问才能配置
 */
@Slf4j
@RestController
@RequestMapping("/dashboard")
public class RateLimitController {

    /**
     * <h2>在 dashboard 流控规则中按照资源名称新增流控规则</h2>
     */
    @GetMapping("/by-resource")
    @SentinelResource(
            value = "byResource",
            blockHandler = "qinyiHandleBlockException",
            blockHandlerClass = QinyiBlockHandler.class
    )
    public CommonResponse<String> byResource(){
        log.info("coming into rate limit controller by resource");

        return new CommonResponse<>(
                0,
                "",
                "By resource"
        );
    }

    /**
     * <h2>在簇点链路中添加流控规则</h2>
     */
    @GetMapping("/by-url")
    @SentinelResource(
            value = "byUrl"
    )
    public CommonResponse<String> byUrl(){
        log.info("coming in rate limit controller by url");
        return new CommonResponse<>(
                0,
                "",
                "byUrl"
        );
    }
}
